package com.booth.manage.model;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.booth.base.model.Users;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/***

 */
public class Booth extends Model<Booth> {

    private static final long serialVersionUID = 1L;
    public static Booth dao = new Booth();

    /***
     * 根据过滤条件获取数据并分页
     * 
     * @param page：当前页
     * @param pageSize：每页条数
     * @param name：内容名称
     * @param beginDate：开始时间
     * @param endDate：结束时间
     * @return 返回满足过滤条件的数据结合，否则放回null
     */
    public Page<Booth> findPage(int page, int pageSize, String name, String num, String status) {

        String whereString = " WHERE 1=1 and t.is_delete = 0 ";
        if (name != null && !name.equals("")) {
            whereString = whereString + " AND t.name like('%" + name + "%')";
        }
        if (num != null && !num.equals("")) {
            whereString = whereString + " AND t.num like('%" + num + "%')";
        }
        if (status != null && !status.equals("")) {
            whereString = whereString + " AND t.status=" + status + "";
        }
        return dao.paginate(page, pageSize, "SELECT t.*  ",
                " FROM booth t " + whereString + " order by t.create_time desc");

    }

    public boolean deleteById(Integer selectId, Users curUser) {
        /*
         * String sql = "update booth set t.status = "+ status; sql +=
         * ",t.updator_id = "+curUser.getUserId(); sql +=
         * ",t.updator_name = "+curUser.getUserName(); sql +=
         * ",t.update_time = "+ new Date
         */
        Record record = new Record().set("is_delete", 1).set("updator_id", curUser.getUserId())
                .set("updator_name", curUser.getUserName()).set("update_time", new Date()).set("id", selectId);

        // Db.update("update booth t set t.status = "+ status);
        return Db.update("booth", "id", record);
    }

    public boolean updateById(Integer selectId, Integer status, Users curUser) {
        /*
         * String sql = "update booth set t.status = "+ status; sql +=
         * ",t.updator_id = "+curUser.getUserId(); sql +=
         * ",t.updator_name = "+curUser.getUserName(); sql +=
         * ",t.update_time = "+ new Date
         */
        Record record = new Record().set("status", status).set("updator_id", curUser.getUserId())
                .set("updator_name", curUser.getUserName()).set("update_time", new Date()).set("id", selectId);

        // Db.update("update booth t set t.status = "+ status);
        return Db.update("booth", "id", record);
    }

}
